<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class All_attendance_model extends CI_Model
{	
	public function __construct()
	{
		parent::__construct();
	} 	
	
  public function get_attendance_details($id)
  {	  
	  $user_id = array(
	  'user_id' => $id,
	  );	  
 		return $query = $this->db->get_where('daily_attendance_sheet', $user_id)->result();		
  }
  
  public function get_all_attendance($year_id,$month_id){  
	$this->db->select('daily_attendance_sheet.*,users.first_name');
	$this->db->from('daily_attendance_sheet');
	$this->db->where('year_id',$year_id);
	$this->db->where('month_id',$month_id);
	$this->db->join('users','daily_attendance_sheet.user_id = users.id');
	$this->db->order_by('users.first_name','ASC');
	return $this->db->get()->result();	
	$this->db->close();
  }
  
  public function get_working_hr($user_id)
	{
		$check = array('user_id'=>$user_id);	
		$this->db->select('min_working_hour, per_day_salary ');
		$this->db->from('salary_account');
		$this->db->where($check);
		return $query = $this->db->get()->row_array();
	}

public function get_user_details($id,$year_id,$month_id)
	{
		$search = array(
		'year_id' =>$year_id,
		'month_id'=>$month_id,
		'user_id' => $id
		);		
		return $this->db->get_where('daily_attendance_sheet',$search)->result();
	}
		
	public function get_details($id,$year_id,$month_id)
	{
		$serach = array(
		'year_id' =>$year_id,
		'month_id'=>$month_id,
		'user_id' => $id
		);		
		return $this->db->get_where('daily_attendance_sheet',$serach)->result();
	}
	
	public function get_time($user_id,$year_id,$month_id,$date_id){
		
		$check = array('year_id' => $year_id, 'month_id' => $month_id, 'date_id' => $date_id,'user_id'=>$user_id,);
		$this->db->select('entry_time, exit_time, entry_flag, exit_flag,lunch_in_time, lunch_in_flag, lunch_out_time, lunch_out_flag');
		$this->db->from('attendance_timings');
		$this->db->where($check);
		return $query = $this->db->get()->row_array();
	}
	
	public function get_attendance_months(){
		$this->db->distinct('month_id');		
		$this->db->select('month_id,year_id');		
		$this->db->from('daily_attendance_sheet');
		//$this->db->get();
		return $this->db->get()->result();
		//return $this->db->last_query();
	}

	public function check_edit_attendance($user_id,$year_id,$month_id){
		$where = array(
		'user_id' => $user_id,
		'year_id' => $year_id,
		'month_id' => $month_id,
		);
		$this->db->select('attendance_timings.date_id');
		$this->db->from('attendance_timings');
		$this->db->where($where);
		$this->db->where('(entry_flag = 1 OR exit_flag = 1)');
		return $this->db->get()->result();
		//$this->db->get();
		//return $this->db->last_query();
	}
	
	
	public function get_worksheet_timings_id($user_id=NULL,$date_id,$month_id,$year_id){
			
		$where = array(
		'user_id' => $user_id,
		'date_id' => $date_id,
		'year_id' => $year_id,
		'month_id' => $month_id,
		);
		
		$this->db->select('worksheet.time_spent');
		$this->db->from('worksheet');
		$this->db->where($where);	
		
		return $this->db->get()->result();		
	}
	public function save_ip($data){		
		$this->db->insert('ip_table',$data);
	}
	
	public function get_ip(){
		$this->db->select('*');
		$this->db->from('ip_table');
		return $this->db->get()->result();
	}
	
	public function delete_ip($id){
		$this->db->delete('ip_table',array('id'=>$id));
	}
	
	public function check_ip($ip){		
		$query  = $this->db->query("SELECT * FROM `ip_table` WHERE ".$ip." BETWEEN `from_ip` AND `to_ip`");		
		$result	= $query->num_rows();		
		if($result > 0){			
			return TRUE;
		}else{			
			return FALSE;
		}
	}
	
		public function get_worksheet_year()
	{
		$this->db->distinct('year_id');
		$this->db->select('year_id');
		$this->db->order_by("year_id","desc");		
		//$this->db->where('user_id', $this->session->userdata('user_id'));
		$query = $this->db->get('attendance_timings')->result();
		return $query;
	}
	
	public function get_worksheet_months()
	{
		$this->db->distinct('month_id');
		$this->db->select('month_id,year_id');
		$this->db->order_by("month_id","desc");
		//$this->db->where('user_id', $this->session->userdata('user_id'));
		return $query = $this->db->get('attendance_timings')->result();
	}
	
	
}
